import json

from flask import Flask
from flask import Response
from flask import request

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

@app.route('/testAjax')
def testAjax():
    res = ["python", "javascript", "ruby", "scala"]
    return json.dumps({
        "code" : 200,
        "msg"  : "success",
        "data" : res
    })

@app.route('/testAjaxPublic')
def testAjaxPublic():
    res = ["python", "javascript", "ruby", "scala"]

    resp = Response(json.dumps({
        "code" : 200,
        "msg"  : "success",
        "data" : res
    }))
    resp.headers['Access-Control-Allow-Origin'] = '*'

    return resp

@app.route('/testAjaxJsonp')
def testAjaxJsonp():
    callback = request.args.get("callback")

    res = ["python", "javascript", "ruby", "scala"]
    _json = json.dumps({
        "code" : 200,
        "msg"  : "success",
        "data" : res
    })

    resp = Response(callback + "(" + _json + ")")
    resp.headers['Content-Type'] = 'text/javascript'

    return resp

@app.route('/testMyJsonp')
def testMyJsonp():
    callback = request.args.get("callback")

    res = ["python", "javascript", "ruby", "scala"]
    _json = json.dumps({
        "code" : 200,
        "msg"  : "success",
        "data" : res
    })

    resp = Response("function " + callback + "(){ return '" + _json + "'}")
    resp.headers['Content-Type'] = 'text/javascript'

    return resp

if __name__ == '__main__':
    app.run(debug=False, host="0.0.0.0", port=5000)
